package com.photography.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.photography.common.Result;
import com.photography.entity.Carousel;
import com.photography.entity.Product;
import com.photography.entity.ProductReview;
import com.photography.service.ProductReviewsService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 商品评论 前端控制器
 * </p>
 *
 * @author
 * @since 2025-05-23
 */
@RestController
@RequestMapping("/review")
public class ProductReviewsController {


    @Resource
    private ProductReviewsService productReviewsService;


    /**
     * 分页查询评论
     */
    @GetMapping("/page")
    public Result getPage(@RequestParam(defaultValue = "1") Integer current,
                          @RequestParam(defaultValue = "10") Integer size,
                          @RequestParam(required = false) String rating
    ) {

        // 创建分页对象
        Page<ProductReview> page = new Page<>(current, size);
        // 执行分页查询
        Page<ProductReview> productReviewPage = productReviewsService.getProductReviewPage(page, rating);
        return Result.success(productReviewPage, "查询成功");
    }

    /**
     * 根据产品ID查询评论
     */
    @GetMapping("/product/{productId}")
    public Result findByProductId(@PathVariable Integer productId) {
        return productReviewsService.findByProductId(productId);
    }

    /**
     * 根据用户ID查询评论
     */
    @GetMapping("/user/{userId}")
    public Result findByUserId(@PathVariable Integer userId) {
        return productReviewsService.findByUserId(userId);
    }

    /**
     * 获取产品的平均评分
     */
    @GetMapping("/product/{productId}/rating")
    public Result getAverageRating(@PathVariable Integer productId) {
        return productReviewsService.getAverageRating(productId);
    }

    /**
     * 获取产品的评论总数
     */
    @GetMapping("/product/{productId}/count")
    public Result getReviewCount(@PathVariable Integer productId) {
        return productReviewsService.getReviewCount(productId);
    }

    /**
     * 根据产品ID查询评论（支持查询参数）
     */
    @GetMapping("/product")
    public Result findByProductIdQuery(@RequestParam Integer productId) {
        return productReviewsService.findByProductId(productId);
    }
}
